﻿using System;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using PlugNT.Safe;
using PlugNT.Common;

namespace PlugNT.Custom
{
    public class WebSeting
    {

        private const string configfilename = "site.config";
        /// <summary>
        /// 判断配置文件是否存在
        /// </summary>
        /// <returns></returns>
        public static bool Exists()
        {
            return System.IO.File.Exists(WebConfig.ConfigPath + configfilename);
        }

        public static bool Exists(string path)
        {
            return System.IO.File.Exists(path);
        }


        /// <summary>
        /// 更新配置
        /// </summary>
        /// <param name="args">NameValue对象</param>
        /// <returns></returns>
        public static string Update(params NameValue[] args)
        {
            string error = "";
            try
            {
                if (args.Length > 0)
                {
                    XmlDataDocument xmlDoc = new XmlDataDocument();
                    xmlDoc.Load(WebConfig.ConfigPath + configfilename);
                    foreach (NameValue config in args)
                    {
                        XmlNode _xml_tmp = xmlDoc.SelectSingleNode(config.Name);
                        if (_xml_tmp != null)
                        {
                            _xml_tmp.InnerXml = config.Value;
                        }
                    }
                    xmlDoc.Save(WebConfig.ConfigPath + configfilename);
                    xmlDoc = null;

                    WebConfig.InitConfig(null);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }

        #region 动态模板
        public static string AddAspxTemplets(params NameValue[] args)
        {
            string error = "";
            try
            {
                if (args.Length > 0)
                {
                    XmlDataDocument xmlDoc = new XmlDataDocument();
                    xmlDoc.Load(WebConfig.ConfigPath + configfilename);
                    XmlNode root = xmlDoc.SelectSingleNode("/configs/templet/aspx");
                    foreach (NameValue config in args)
                    {
                        XmlNode node = xmlDoc.SelectSingleNode("/configs/templet/aspx/item[@name='" + config.Name + "']");
                        if (node == null)
                        {
                            XmlElement xe1 = xmlDoc.CreateElement("item");   //创建一个节点
                            xe1.SetAttribute("name", config.Name);
                            xe1.InnerText = config.Value;
                            root.AppendChild(xe1);
                        }
                    }
                    xmlDoc.Save(WebConfig.ConfigPath + configfilename);
                    xmlDoc = null;

                    WebConfig.InitConfig(null);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }
        public static string UpdateAspxTemplets(params NameValue[] args)
        {
            string error = "";
            try
            {
                if (args.Length > 0)
                {
                    XmlDataDocument xmlDoc = new XmlDataDocument();
                    xmlDoc.Load(WebConfig.ConfigPath + configfilename);
                    foreach (NameValue config in args)
                    {
                        XmlNode node = xmlDoc.SelectSingleNode("/configs/templet/aspx/item[@name='" + config.Name + "']");
                        if (node != null)
                        {
                            node.InnerXml = config.Value;
                        }
                    }
                    xmlDoc.Save(WebConfig.ConfigPath + configfilename);
                    xmlDoc = null;

                    WebConfig.InitConfig(null);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }
        public static string DeleteAspxTemplets(params NameValue[] args)
        {
            string error = "";
            try
            {
                if (args.Length > 0)
                {
                    XmlDataDocument xmlDoc = new XmlDataDocument();
                    xmlDoc.Load(WebConfig.ConfigPath + configfilename);
                    XmlNode root = xmlDoc.SelectSingleNode("/configs/templet/aspx");
                    foreach (NameValue config in args)
                    {
                        XmlNode node = xmlDoc.SelectSingleNode("/configs/templet/aspx/item[@name='" + config.Name + "']");
                        if (node != null)
                        {
                            root.RemoveChild(node);
                        }
                    }
                    xmlDoc.Save(WebConfig.ConfigPath + configfilename);
                    xmlDoc = null;

                    WebConfig.InitConfig(null);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }
        #endregion


        #region SQL语句
        public static string AddSqlKeyValues(params NameValue[] args)
        {
            string error = "";
            try
            {
                if (args.Length > 0)
                {
                    XmlDataDocument xmlDoc = new XmlDataDocument();
                    xmlDoc.Load(WebConfig.ConfigPath + configfilename);
                    XmlNode root = xmlDoc.SelectSingleNode("/configs/db/sql");
                    foreach (NameValue config in args)
                    {
                        XmlNode node = xmlDoc.SelectSingleNode("/configs/db/sql/item[@name='" + config.Name + "']");
                        if (node == null)
                        {
                            XmlElement xe1 = xmlDoc.CreateElement("item");   //创建一个节点
                            xe1.SetAttribute("name", config.Name);
                            xe1.InnerText = config.Value;
                            root.AppendChild(xe1);
                        }
                    }
                    xmlDoc.Save(WebConfig.ConfigPath + configfilename);
                    xmlDoc = null;

                    WebConfig.InitConfig(null);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }
        public static string UpdateSqlKeyValues(params NameValue[] args)
        {
            string error = "";
            try
            {
                if (args.Length > 0)
                {
                    XmlDataDocument xmlDoc = new XmlDataDocument();
                    xmlDoc.Load(WebConfig.ConfigPath + configfilename);
                    foreach (NameValue config in args)
                    {
                        XmlNode node = xmlDoc.SelectSingleNode("/configs/db/sql/item[@name='" + config.Name + "']");
                        if (node != null)
                        {
                            node.InnerXml = config.Value;
                        }
                    }
                    xmlDoc.Save(WebConfig.ConfigPath + configfilename);
                    xmlDoc = null;

                    WebConfig.InitConfig(null);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }
        public static string DeleteSqlKeyValues(params NameValue[] args)
        {
            string error = "";
            try
            {
                if (args.Length > 0)
                {
                    XmlDataDocument xmlDoc = new XmlDataDocument();
                    xmlDoc.Load(WebConfig.ConfigPath + configfilename);
                    XmlNode root = xmlDoc.SelectSingleNode("/configs/db/sql");
                    foreach (NameValue config in args)
                    {
                        XmlNode node = xmlDoc.SelectSingleNode("/configs/db/sql/item[@name='" + config.Name + "']");
                        if (node != null)
                        {
                            root.RemoveChild(node);
                        }
                    }
                    xmlDoc.Save(WebConfig.ConfigPath + configfilename);
                    xmlDoc = null;

                    WebConfig.InitConfig(null);
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }
        #endregion


        /// <summary>
        /// 设置支付宝信息
        /// </summary>
        /// <param name="t_alipaykey"></param>
        /// <param name="t_alipayselleremail"></param>
        /// <param name="t_alipaypartner"></param>
        /// <param name="t_alipaytype"></param>
        /// <returns></returns>
        public static string UpdateAlipay(string t_alipaykey, string t_alipayselleremail, string t_alipaypartner, string t_alipaytype)
        {
            string error = "";
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(WebConfig.ConfigPath + configfilename);
                xml.SelectSingleNode("/configs/alipay/selleremail").InnerXml = Encrypter.DESEncrypt(t_alipayselleremail, WebConfig.alipaysecurity);
                xml.SelectSingleNode("/configs/alipay/key").InnerXml = Encrypter.DESEncrypt(t_alipaykey, WebConfig.alipaysecurity);
                xml.SelectSingleNode("/configs/alipay/partner").InnerXml = Encrypter.DESEncrypt(t_alipaypartner, WebConfig.alipaysecurity);
                xml.SelectSingleNode("/configs/alipay/type").InnerXml = Encrypter.DESEncrypt(t_alipaytype, WebConfig.alipaysecurity);
                xml.Save(WebConfig.ConfigPath + configfilename);

                WebConfig.InitAlipay(null);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }


        /// <summary>
        /// 设置自动更新页面
        /// </summary>
        /// <param name="flag">是否</param>
        /// <param name="datespan">时间间隔</param>
        public static string UpdateAutoUpdate(bool flag, string datespan)
        {
            string error = "";
            try
            {
                //PlugNT.Cms.AutoUpdate.Start();
                XmlDocument xml = new XmlDocument();
                xml.Load(WebConfig.ConfigPath + configfilename);
                xml.SelectSingleNode("/configs/templet/autoupdate/locked").InnerText = flag.ToString();
                xml.SelectSingleNode("/configs/templet/autoupdate/datespan").InnerText = datespan;
                xml.Save(WebConfig.ConfigPath + configfilename);

                WebConfig.InitAutoUpdate(null);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }


        /// <summary>
        /// 用户表自定义加密方式
        /// </summary>
        /// <param name="hashText"></param>
        /// <returns></returns>
        public static string UserHasher(string hashText)
        {
            return PlugNT.Safe.Encrypter.MD5(hashText);
        }

    }
}


#region 获取添加修改删除配置信息函数(过时)
/*
        public static void InitConfigs()
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(ConfigPath + configfilename);
            XmlNode root = xml.SelectSingleNode("configs");
            foreach (XmlNode n in root.ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "item")
                {
                    XmlAttribute name = n.Attributes["name"];
                    if (name != null)
                    {
                        string nameStr = name.Value.Trim();
                        string valueStr = n.Attributes["value"].Value;    //DESEncrypter.Decrypt(n.Attributes["value"].Value);
                        switch (nameStr)
                        {
                            case "DbType":
                                _dbtype = valueStr;
                                break;
                            case "ConnectionString":
                                _connectionString = valueStr;
                                break;
                            default:
                                break;
                        }

                    }
                }
            }
        }
        public static string GetValue(string itemName)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(ConfigPath);
            XmlNode root = xml.SelectSingleNode("configs");
            string str = "";
            foreach (XmlNode n in root.ChildNodes)
            {
                if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "item")
                {
                    XmlAttribute name = n.Attributes["name"];
                    if (name != null && name.Value.Trim() == itemName)
                    {
                        str = n.Attributes["value"].Value;
                        break;
                    }
                }
            }
            return str;
        }
        public static string AddItem(string itemName, string itemValue)
        {
            string error = "";
            try
            {
                XmlDataDocument xmlDoc = new XmlDataDocument();
                xmlDoc.Load(ConfigPath);
                XmlNode root = xmlDoc.SelectSingleNode("configs");//查<LoginHs>
                XmlElement xe1 = xmlDoc.CreateElement("item");//创建一个接点
                xe1.SetAttribute("name", itemName);
                xe1.SetAttribute("value", itemValue);

                root.AppendChild(xe1);  //添加到<bookstore>节点中
                xmlDoc.Save(ConfigPath);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }

        public static string ModifyItem(string itemName, string itemValue)
        {
            string error = "";
            try
            {
                XmlDataDocument xmlDoc = new XmlDataDocument();
                xmlDoc.Load(ConfigPath);
                XmlNode root = xmlDoc.SelectSingleNode("configs");//查<LoginHs>
                XmlElement xe1 = xmlDoc.CreateElement("item");//创建一个接点
                xe1.SetAttribute("name", itemName);
                xe1.SetAttribute("value", itemValue);

                foreach (XmlNode n in root.ChildNodes)
                {
                    if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "item")
                    {
                        XmlAttribute name = n.Attributes["name"];
                        if (name != null && name.Value.Trim() == itemName)
                        {
                            root.ReplaceChild(xe1, n);
                            break;
                        }
                    }
                }
                xmlDoc.Save(ConfigPath);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }

        public static string DeleteItem(string itemName)
        {
            string error = "";
            try
            {
                XmlDataDocument xmlDoc = new XmlDataDocument();
                xmlDoc.Load(ConfigPath);
                XmlNode root = xmlDoc.SelectSingleNode("configs");//查<LoginHs>
                foreach (XmlNode n in root.ChildNodes)
                {
                    if (n.NodeType != XmlNodeType.Comment && n.Name.ToLower() == "item")
                    {
                        XmlAttribute name = n.Attributes["name"];
                        if (name != null && name.Value.Trim() == itemName)
                        {
                            root.RemoveChild(n);
                            break;
                        }
                    }
                }
                xmlDoc.Save(ConfigPath);
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            return error;
        }
        */
#endregion